<?php
require_once('admin_inc_path.php');

if (isset($_POST['save'])) {

    $response = DAOFactory::getMainMenuDAO()->load($_POST['segment']);
    if (empty($response)):
        $mh = new MainHeading();
        $link1 = new UrlLink();
        $link2 = new UrlLink();
        $mm = new MainMenu();
    else:
        $mh->id = $response->mH;
        $link1->id = $response->link1;
        $link2->id = $response->link2;
    endif;

    $mh->textEn = $_POST['mh_en'];
    $mh->textSv = $_POST['mh_sv'];
    $link1->linkEn = $_POST['l1_en'];
    $link1->linkSv = $_POST['l1_sv'];
    $link2->linkEn = $_POST['l2_en'];
    $link2->linkSv = $_POST['l2_sv'];
    $mm->id = $_POST['segment'];

    if ($response):

        $resp1 = DAOFactory::getUrlLinkDAO()->update($link1);
        $resp2 = DAOFactory::getUrlLinkDAO()->update($link2);
        $resp3 = DAOFactory::getMainHeadingDAO()->update($mh);
        if($resp1||$resp2||resp3):
            echo __UPDATED__;
        endif;
        //echo 'info updated:--';
        //var_dump($resp1, $resp2, $resp3); //for debugging  

    else:
        $mm->link1 = DAOFactory::getUrlLinkDAO()->insert($link1);
        $mm->link2 = DAOFactory::getUrlLinkDAO()->insert($link2);
        $mm->mH = DAOFactory::getMainHeadingDAO()->insert($mh);
        $resp = DAOFactory::getMainMenuDAO()->insert($mm);
        //echo 'info inserted:--';
        //var_dump($resp); //for debugging
        if($mm->link1||$mm->link2||$mm->mH||$resp):
            echo __INSERTED__;
        endif;

    endif;
}
?>




<?php include_once('inc/admin_header.php') ?>		
<div class="admin_content_wrapper">
    <h1>Top Menu</h1>


    <?php
    for ($i = 1; $i < 6; $i++):

        $mm = DAOFactory::getMainMenuDAO()->load($i);
        $mh = 'mh' . $i;
        $$mh = DAOFactory::getMainHeadingDAO()->load($mm->mH);
        $l1 = 'l1' . $i;
        $$l1 = DAOFactory::getUrlLinkDAO()->load($mm->link1);
        $l2 = 'l2' . $i;
        $$l2 = DAOFactory::getUrlLinkDAO()->load($mm->link2);
        ?>
        <h2>Segment <?php echo $i ?> (from left)</h2>
        <form action="" method="POST" >
            <div class="admin_segment">
                <table>
                    <tr>
                        <td>

                        </td>
                        <td>
                            <h3>Svenska</h3>
                        </td>
                        <td>
                            <h3>Engelska</h3>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <h3>Heading:</h3>
                        </td>
                        <td>
                            <input type="text" name="mh_sv" value="<?php echo $value = (!empty($$mh->textSv)) ? $$mh->textSv : __EMT; ?>"/>
                        </td>
                        <td>
                            <input type="text" name="mh_en" value="<?php echo $value = (!empty($$mh->textEn)) ? $$mh->textEn : __EMT; ?>"/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <h3>Link Name 1:</h3>
                        </td>
                        <td>
                            <input type="text" name="l1_sv" value="<?php echo $value = (!empty($$l1->linkSv)) ? $$l1->linkSv : __EMT; ?>"/>
                        </td>
                        <td>
                            <input type="text" name="l1_en" value="<?php echo $value = (!empty($$l1->linkEn)) ? $$l1->linkEn : __EMT; ?>"/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <h3>Link Name 2:</h3>
                        </td>
                        <td>
                            <input type="text" name="l2_sv" value="<?php echo $value = (!empty($$l2->linkSv)) ? $$l2->linkSv : __EMT; ?>" />
                        </td>
                        <td>
                            <input type="text" name="l2_en" value="<?php echo $value = (!empty($$l2->linkEn)) ? $$l2->linkEn : __EMT; ?>" />
                        </td>
                    </tr>
                </table>
                <input type="hidden" name="segment" value="<?php echo $i ?>" />
                <input type="submit" value="Save" name="save"/>
            </div>
        </form>
        <?php
    endfor;
    ?>         	

</div>
<?php include_once('inc/admin_footer.php') ?>